package com.lottery.obtainData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.utils.HttpRequestUtil;

public class ObtainDataUtils {

	// 获得接口数据
	public static String obtainData(String url) {
		String httpRequest = null;
		try {
			httpRequest = HttpRequestUtil.httpRequest(url);
		} catch (Exception e) {
			return null;
		}
		return httpRequest;
	}

	// 字符串转Json
	public static JSONObject stringToJson(String string) {
		JSONObject parseObject = null;
		try {
			parseObject = JSON.parseObject(string);
		} catch (Exception e) {
			return null;
		}
		return parseObject;
	}

	// 通过下一期信息获得下一期开奖期号
	public static String getNextExpectByNum(JSONObject nextObject) {
		String nextExpect = "";
		String nextExpectNum = nextObject.getString("periodNumber");
		String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
		if (nextExpectNum.length() == 1) {
			nextExpect = format + "00" + nextExpectNum;
		} else if (nextExpectNum.length() == 2) {
			nextExpect = format + "0" + nextExpectNum;
		} else {
			nextExpect = format + nextExpectNum;
		}
		return nextExpect;
	}

	// 休眠
	public static boolean sleep(String nextTime, int advanceTime) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			long time = simpleDateFormat.parse(nextTime).getTime() - advanceTime;
			long sleeptime = time - new Date().getTime();
			sleeptime = sleeptime > 0 ? sleeptime : 0;
			Thread.sleep(sleeptime);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	// 通过2号接口获取的字符串提取数据
	public static Map<String, String> extractingData(String httpRequest) {
		// 当前开奖期号
		int indexOf = httpRequest.indexOf("20");
		String expect = httpRequest.substring(indexOf, indexOf + 11);
		// logger.info("2号接口当前开奖期号={}", expect);

		// 当前开奖时间
		String substring = httpRequest.substring(indexOf + 11, httpRequest.length() - 1);
		int indexOf2 = substring.indexOf("\\uff1a");
		String expectTime = substring.substring(indexOf2 + 6, indexOf2 + 25);
		// logger.info("2号接口当前开奖时间={}", expectTime);

		// 当前开奖号码
		int i = 5;
		StringBuffer stringBuffer = new StringBuffer();
		while (i > 0) {
			int indexOf3 = substring.indexOf("hq_bg");
			stringBuffer.append(substring.substring(indexOf3 + 8, indexOf3 + 9) + ",");
			substring = substring.substring(indexOf3 + 9, substring.length() - 1);
			i--;
		}
		String string = stringBuffer.toString();
		String openCode = string.substring(0, string.length() - 1);
		// logger.info("2号接口当前开奖号码={}", openCode);

		// 下次开奖的期号
		String nextExpect = "";
		String nextExpectNum = expect.substring(8);
		if (nextExpectNum.equals("120")) {
			nextExpectNum = "1";
		} else {
			nextExpectNum = Integer.valueOf(nextExpectNum) + 1 + "";
		}
		String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
		if (nextExpectNum.length() == 1) {
			nextExpect = format + "0" + "0" + nextExpectNum;
		} else if (nextExpectNum.length() == 2) {
			nextExpect = format + "0" + nextExpectNum;
		} else {
			nextExpect = format + nextExpectNum;
		}
		// logger.info("2号接口下期开奖期号={}", nextExpect);

		// 下次开奖的时间
		String nextTime = "";
		String formatDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String formatMorning = formatDate + " 00:00:00";
		String formatForenoon = formatDate + " 10:00:00";
		String formatEvening = formatDate + " 22:00:00";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long timeMorning = 0;
		long timeForenoon = 0;
		long timeEvening = 0;
		long timeNow = 0;
		try {
			timeMorning = simpleDateFormat.parse(formatMorning).getTime();
			timeForenoon = simpleDateFormat.parse(formatForenoon).getTime();
			timeEvening = simpleDateFormat.parse(formatEvening).getTime();
		} catch (ParseException e) {
			// logger.info("2号接口错误={}", "日期转换出错");
		}
		Integer NextExpectNumber = Integer.valueOf(nextExpectNum);
		if (NextExpectNumber >= 1 && NextExpectNumber <= 23) {
			timeNow = timeMorning + 300000 * NextExpectNumber;
		}
		if (NextExpectNumber >= 24 && NextExpectNumber <= 96) {
			timeNow = timeForenoon + 600000 * (NextExpectNumber - 24);
		}
		if (NextExpectNumber >= 97 && NextExpectNumber <= 120) {
			timeNow = timeEvening + 300000 * (NextExpectNumber - 96);
		}
		nextTime = simpleDateFormat.format(timeNow);
		// logger.info("2号接口下次开奖的时间={}", nextTime);

		Map<String, String> extractData = new HashMap<>();
		extractData.put("expect", expect);
		extractData.put("openTime", expectTime);
		extractData.put("openCode", openCode);
		extractData.put("nextExpect", nextExpect);
		extractData.put("nextTime", nextTime);

		return extractData;
	}

	public static String getNextExpect(String expect) {
		String nextExpect = "";
		String nextExpectNum = expect.substring(8);
		if (nextExpectNum.equals("120")) {
			nextExpectNum = "1";
		} else {
			nextExpectNum = Integer.valueOf(nextExpectNum) + 1 + "";
		}
		String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
		if (nextExpectNum.length() == 1) {
			nextExpect = format + "0" + "0" + nextExpectNum;
		} else if (nextExpectNum.length() == 2) {
			nextExpect = format + "0" + nextExpectNum;
		} else {
			nextExpect = format + nextExpectNum;
		}
		return nextExpect;
	}

	public static String getFullPeriod(Date date) {

		SimpleDateFormat format = new SimpleDateFormat("YYYYMMdd");

		String str = format.format(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int minites = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
		return formatFullPeoriod(str, getPeriod(minites));

	}

	private static String formatFullPeoriod(String str, int period) {
		if (period < 10) {
			return str + "00" + period;
		} else if (period < 100) {
			return str + "0" + period;
		}
		return str + period;
	}

	private static int getPeriod(int minites) {
		int period = 0;
		if (minites <= 23 * 5) {
			period = minites / 5 + 1;
			return period;
		} else if (minites <= 10 * 60) {
			period = 23;
			return period;
		} else {
			period = (minites - 10 * 60) / 10 + 24;
		}
		return period;
	}

}
